﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using System.Globalization;
using Shpm.App.Commons.Utils;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text.RegularExpressions;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
public partial class App_Input_AccountPrivatePrintForm : System.Web.UI.Page
{
    private string memberNo ;
    private string beginDate;
    private string endDate ;
    private string commitFlg;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
             memberNo = Convert.ToString(Request.QueryString["MEMBERNO"]);
             beginDate = Convert.ToString(Request.QueryString["BEGINDATE"]);
             endDate = Convert.ToString(Request.QueryString["ENDDATE"]);
             commitFlg = Convert.ToString(Request.QueryString["COMMITFLG"]);
        }
        this.ViewState["MEMBER_NO"] =memberNo;
        this.ViewState["BEGIN_DATE"]=Convert.ToDateTime(beginDate);
        this.ViewState["END_DATE"] =Convert.ToDateTime(endDate);
        this.ViewState["COMMIT_FLG"] = commitFlg;

        setPage();
    }
    private void setPage()
    {
        //数据读取
        AccountPrivateBean dataBean = new AccountPrivateBean();
        AccountPrivateDao Dao = new AccountPrivateDao();
        AccountPrivateBean apBean = new AccountPrivateBean();
        AccountPrivateBean sumBean = new AccountPrivateBean();

        dataBean.MemberNo = memberNo;
        sumBean.MemberNo = memberNo;
        if (!commitFlg.Trim().Equals(string.Empty))
        {
            if (commitFlg == "1")
            {
                dataBean.CommitFlg = true;
                sumBean.CommitFlg = true;
            }
            else
            {
                dataBean.CommitFlg = false;
                sumBean.CommitFlg = false;
            }
        }
        dataBean.BeginDate = Convert.ToDateTime(beginDate);
        dataBean.EndDate = Convert.ToDateTime(endDate).AddDays(1);
        sumBean.BeginDate = Convert.ToDateTime(beginDate);
        sumBean.EndDate = Convert.ToDateTime(endDate).AddDays(1);
        IList dataList = Dao.ListAccountPrivate(dataBean);
        IList sumdataList = Dao.SumAccountPrivate(sumBean);
        if (sumdataList.Count > 0)
        {
            sumBean = (AccountPrivateBean)sumdataList[0];
        }
        if (dataList.Count > 0)
        {
            
            apBean = (AccountPrivateBean)dataList[0];
        }

        //页面设置
        MemoryStream stream = new MemoryStream();


        Document document = new Document(PageSize.A4, 80, 50, 30, 65);



        PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
        document.Open();


        FontFactory.RegisterDirectory("c:\\Windows\\Fonts");

        FontSelector selector = new FontSelector();

        Font font = FontFactory.GetFont("simsun", BaseFont.IDENTITY_H, 16, Font.BOLD, new Color(0, 0, 0));

        selector.AddFont(font);



        Paragraph title1 = new Paragraph(selector.Process("个人详细消费信息"));


        title1.Alignment = Element.ALIGN_CENTER;

        document.Add(title1);

        PdfPTable table = new PdfPTable(5);
        PdfPCell cell = new PdfPCell(new Paragraph(" "));
        cell.Border = 0;
        cell.Colspan = 5;
        table.AddCell(cell);
        cell.Colspan = 2;

        font.Size = 10;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        cell.Phrase = selector.Process("房间号： " + apBean.ApartmentNo);
        table.AddCell(cell);

        cell.Colspan = 3;
        cell.Phrase = selector.Process("会员卡号：");
        table.AddCell(cell);

        cell.Colspan = 2;
        cell.Phrase = selector.Process("会员编号： " + apBean.MemberNo);
        table.AddCell(cell);

        cell.Colspan = 3;
        cell.Phrase = selector.Process("会员姓名：" + apBean.NameC);
        table.AddCell(cell);

        cell.Colspan = 5;
        cell.Phrase = selector.Process("账单总额：" +string.Format("{0:N}",sumBean.SumAmount));
        table.AddCell(cell);

        cell.Colspan = 5;
        cell.Phrase = selector.Process("");
        table.AddCell(cell);
        document.Add(table);
        float[] widths ={ 0.7f, 1f, 2f, 0.8f, 0.5f };
        table = new PdfPTable(widths);
        cell = new PdfPCell(new Paragraph(" "));
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        cell.Colspan = 1;
        cell.Phrase = selector.Process("账单日期");
        table.AddCell(cell);

        cell.Colspan = 1;
        cell.Phrase = selector.Process("消费类型");
        table.AddCell(cell);

        cell.Colspan = 1;
        cell.Phrase = selector.Process("消费内容");
        table.AddCell(cell);

        cell.Colspan = 1;
        cell.Phrase = selector.Process("金额/元");
        table.AddCell(cell);

        cell.Colspan = 1;
        cell.Phrase = selector.Process("状态");
        table.AddCell(cell);

        font = FontFactory.GetFont("simsun", BaseFont.IDENTITY_H,8, Font.NORMAL, new Color(0, 0, 0));
        selector = new FontSelector();
        selector.AddFont(font);

        for (int i = 0; i < dataList.Count; i++)
        {

            dataBean = (dataList[i] as AccountPrivateBean);
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.Colspan = 1;
            cell.Phrase = selector.Process(Convert.ToDateTime(dataBean.InvoiceDate).ToShortDateString());
            table.AddCell(cell);

            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.Colspan = 1;
            if (dataBean.InvoiceType == "DINNER")
            {
                cell.Phrase = selector.Process("餐饮");
            }
            else if (dataBean.InvoiceType == "SPECIAL")
            {
                cell.Phrase = selector.Process("代买代办");
            }
            table.AddCell(cell);

            cell.Colspan = 1;
            cell.Phrase = selector.Process(dataBean.InvoiceContent);
            table.AddCell(cell);

            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            cell.Colspan = 1;
            cell.Phrase = selector.Process(string.Format("{0:N}",dataBean.Amount));
            table.AddCell(cell);

            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.Colspan = 1;
            if (dataBean.InvoiceFlg)
            {
                cell.Phrase = selector.Process("已付");
            }
            else 
            {
                cell.Phrase = selector.Process("未付");
            }
            table.AddCell(cell);
        }

        document.Add(table);
        document.Close();





        Response.AddHeader("Cache-Control", "cache");
        Response.AddHeader("Content-Type", "application/pdf");
        Response.AddHeader("Content-Length", stream.GetBuffer().Length.ToString());
        Response.BinaryWrite(stream.GetBuffer());

        Response.End();
    }
  
}
